/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component.textview.supertextview.adapter;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * 扩展Text
 *
 * @since 2021-04-07
 */
public class OfText extends Text implements Text.TextObserver {
    private int maxLength = Integer.MAX_VALUE;

    /**
     * 构造器
     *
     * @param context 上下文
     */
    public OfText(Context context) {
        this(context, null);
    }

    /**
     * 从xml构造
     *
     * @param context 上下文
     * @param attrSet 属性集
     */
    public OfText(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * 构造器
     *
     * @param context 上下文
     * @param attrSet 属性集
     * @param styleName 样式名
     */
    public OfText(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
    }

    private void init() {
        addTextObserver(this);
    }

    /**
     * 设置最大长度
     *
     * @param maxLength 最大长度
     * @throws IllegalArgumentException maxLength不能为空,否则将抛出此错误
     */
    public void setMaxLength(int maxLength) {
        if (maxLength < 0) {
            throw new IllegalArgumentException("The maxLength must greater than 0");
        }
        this.maxLength = maxLength;
        fixMaxLength();
    }

    private void fixMaxLength() {
        if (getLength() > getMaxLength()) {
            setText(getText().substring(0, getLength() - 1));
        }
    }

    @Override
    public void onTextUpdated(String s, int i, int i1, int i2) {
        fixMaxLength();
    }

    public int getMaxLength() {
        return maxLength;
    }

    /**
     * 返回最大长度
     *
     * @return 最大长度
     */
    public int getLength() {
        String text = getText();
        return text != null ? text.length() : 0;
    }

    /**
     * 设置行数
     *
     * @param lines 行数
     */
    public void setLines(int lines) {
        setMaxLength(lines);
    }

    /**
     * 设置文字颜色
     *
     * @param color int类型颜色
     */
    public void setTextColor(int color) {
        setTextColor(new Color(color));
    }

    /**
     * 设置文字环绕图片
     *
     * @param left 左边图片
     * @param top 上图片
     * @param right 右方图片
     * @param bottom 下方图片
     */
    public void setCompoundElements(Element left, Element top, Element right, Element bottom) {
        setAroundElements(left, top, right, bottom);
    }

    /**
     * 设置字体
     *
     * @param font 字体
     */
    public void setTypeface(Font font) {
        setFont(font);
    }
}
